package com.feng.show.service.impl;

import com.feng.show.comm.ServerResponse;
import com.feng.show.dao.UserInfoMapper;
import com.feng.show.entity.UserInfo;
import com.feng.show.entity.UserInfoExample;
import com.feng.show.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.feng.show.comm.ServerResponse.createBySuccesMsg;
import static com.feng.show.comm.ServerResponse.createBySuccessMsg;

@Service("IUserInfoService")
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper mapper;


    @Override
    public ServerResponse<List<UserInfo>> getAllUerInfo(String uerId) {
       List<UserInfo> list= mapper.selectByExample(null);
       if(list!=null&&list.size()>0){
        return  ServerResponse.createBySuccesMsg(list);
       }else{
           return  ServerResponse.createBySuccessMsg("无数据");
       }


    }

    @Override
    public ServerResponse<Boolean> addUser(String uName, String pwd, String mobile, Integer type) {

        if(uName==null||pwd==null||mobile==null||uName.length()<3||pwd.length()<3){
            return ServerResponse.createByErrorMsg("参数错误");
        }


        //首先查找，用户是否存在，


        UserInfoExample userInfoExample=new UserInfoExample();
        UserInfoExample.Criteria criteria=userInfoExample.createCriteria();
        criteria.andUserNameEqualTo(uName);

        List<UserInfo> list=mapper.selectByExample(userInfoExample);
        if(list!=null&&list.size()>0){

            return ServerResponse.createBySuccessMsg("用户已存在");
        }


        UserInfo userInfo=new UserInfo();
        userInfo.setUserName(uName);
        userInfo.setPasswd(pwd);
        userInfo.setMoble(mobile);
        userInfo.setType(type);
        int  status=mapper.insert(userInfo);

//        Integer id=userInfo.getId();
        if(status>0){
          return  ServerResponse.createBySuccessMsg("提交成功");
        }else{
            return ServerResponse.createBySuccessMsg("提交失败");
        }
    }

    @Override
    public ServerResponse<Boolean> delUser(Integer userId) {
         int status = 0;
        if(userId!=null){
          status= mapper.deleteByPrimaryKey(userId);
        }

        if(status>0){
            return ServerResponse.createBySuccessMsg("删除成功");
        }else{
            return ServerResponse.createByErrorMsg("删除失败");
        }

    }
}
